import { useUserStore, useUserStores } from '@/stores'
import {
  createRouter,
  createWebHistory,
  createWebHashHistory
} from 'vue-router'
const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      redirect: '/Mywarning'
    },
    {
      path: '/login',
      component: () => import('@/views/login/index.vue'),
      meta: { title: '登录' }
    },
    {
      path: '/logins',
      component: () => import('@/views/logins/index.vue'),
      meta: { title: '登录' }
    },
    {
      path: '/Mywarning',
      component: () => import('@/views/user/Mywarning.vue'),
      meta: { title: '我的预警' }
    },
    {
      path: '/detailPage',
      component: () => import('@/views/detailPage/index.vue'),
      meta: { title: '预警详情' }
    }
  ]
})
const flag = import.meta.env.VITE_APP_FLAG

//前置路由守卫
router.beforeEach((to) => {
  const store = useUserStore()
  const stores = useUserStores()
  if (flag === 'dev') {
    // 白名单
    const whiteList = ['/login', '/detailPage', '/Mywarning']
    // 判断是否登录且不在白名单中
    if (!store.user?.token && !whiteList.includes(to.path)) {
      return '/login'
    }
  } else {
    const whiteList = ['/logins', '/detailPage', '/Mywarning']
    if (!stores.users?.token && !whiteList.includes(to.path)) {
      //做了区分可以不用这个页面的逻辑
      return '/logins'
    }
  }
})
//后置路由守卫
router.afterEach((to) => {
  //关闭进度条
  //NProgress.done()
  if (flag === 'dev') {
    //修改页面标题
    document.title = `${to.meta.title || ''}-预警H5`
  }
})
export default router
